package com.example.smart_city_0829.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smart_city_0829.R;

import java.util.List;

public abstract class CommonAdapter<T, H> extends android.widget.BaseAdapter {

    protected List<T> data;
    protected Context context;

    public CommonAdapter(List<T> data, Context context) {
        this.data = data;
        this.context = context;
    }

    public void resetData(List<T> data) {
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        H viewHolder = null;
        if (convertView == null) {
            viewHolder = getViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(getItemLayoutResourceId(), parent, false);
            initView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (H) convertView.getTag();
        }
        setData(data.get(position), position, viewHolder, convertView, parent.getContext());
        return convertView;
    }

    protected abstract void setData(T data, int position, H viewHolder, View convertView, Context context);

    protected abstract H getViewHolder();

    protected abstract void initView(H viewHolder, View convertView);

    protected int getItemLayoutResourceId() {
        return R.layout.common_list_item;
    }

    /**
     * 图片及介绍
     *
     viewHolder.title = (TextView) convertView.findViewById(R.id.title);
     viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
     viewHolder.content = (TextView) convertView.findViewById(R.id.content);
     viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
     viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
     viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
     */
    public static class CommListViewHolder {
        public TextView title;
        public ImageView im;
        public TextView content;
        public TextView tag1;
        public TextView tag2;
        public TextView tag3;
    }

    /**
     * 宫格列表
     *
     R.layout.server_list_item

     viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
     viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
     */
    public static class CommGridViewHolder {
        public ImageView im;
        public TextView serverName;
    }

    /**
     * 纯text

     R.layout.text

     viewHolder.tv = (TextView) convertView.findViewById(R.id.text);
     *
     */
    public static class CommTextViewViewHolder {
        public TextView tv;
    }

    /**
     * 列表上有点赞按钮
     */
    public static class CommentListViewHolder {
        public ImageView im;
        public TextView name;
        public TextView content;
        public TextView tag1;
        public CheckBox cb;
    }

    /**
     * 列表上有按钮

     R.layout.lawyer_list_item

     viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
     viewHolder.name = (TextView) convertView.findViewById(R.id.name);
     viewHolder.workYearsTv = (TextView) convertView.findViewById(R.id.work_years_tv);
     viewHolder.goodAtTv = (TextView) convertView.findViewById(R.id.goodAt_tv);
     viewHolder.serviceTimesTv = (TextView) convertView.findViewById(R.id.serviceTimes_tv);
     viewHolder.favorableRateTv = (TextView) convertView.findViewById(R.id.favorableRate_tv);
     viewHolder.quesBt = (Button) convertView.findViewById(R.id.ques_bt);

     */
    public static class CommListViewHolder2 {
        public ImageView imageView;
        public TextView name;
        public TextView workYearsTv;
        public TextView goodAtTv;
        public TextView serviceTimesTv;
        public TextView favorableRateTv;
        public Button quesBt;
    }

    /**
     * 单图片列表
     */
    public static class ImViewHolder {
        public ImageView im;
    }
}
